package com.twetec.cms.adapter.pc.backend.tag;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Maps;
import com.twetec.cms.adapter.pc.backend.tag.annotation.TplTag;
import com.twetec.cms.adapter.pc.backend.tag.doc.TagDocObject;
import com.twetec.cms.common.constant.ArticleEnum;
import com.twetec.cms.common.util.validate.ValidateUtil;
import com.twetec.cms.model.po.website.*;
import com.twetec.cms.service.website.*;
import org.apache.commons.lang3.StringUtils;
import org.beetl.core.tag.GeneralVarTagBinding;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service()
@Scope("prototype")
@TplTag("articleAttachmentList")
public class ArticleAttachmentListTag extends GeneralVarTagBinding {

    @Autowired
    private ArticleService articleService;

    @Autowired
    private ArticleAttachmentService attachmentService;

    @Override
    public void render() {
        System.out.println("ArticleAttachmentListTag");

        // 接收标签参数
        Map result = Maps.newHashMap();
        // 文章id
        Long articleId = (this.getAttributeValue("articleId") instanceof String) ?
                Long.parseLong((String) this.getAttributeValue("articleId")) : (Long) this.getAttributeValue("articleId");

//        String serverUrl = (String) this.getAttributeValue("serverUrl");

//        String pageType = (String) this.getAttributeValue("pageType");

        // 查询文章附件列表
        QueryWrapper<ArticlePO> articlePOQueryWrapper = new QueryWrapper<>();
        articlePOQueryWrapper.eq("ID", articleId);
        articlePOQueryWrapper.eq("IS_RECOVERY", 0);
        ArticlePO articlePO = articleService.getOne(articlePOQueryWrapper);

        if (!ValidateUtil.isNullOrEmpty(articlePO)) {
            // 附件列表(xxx:[{xxx:xxx,xxx:xxx}, {xxx:xxx,xxx:xxx}])
            QueryWrapper<ArticleAttachmentPO> articleAttachmentPOQueryWrapper = new QueryWrapper<>();
            articleAttachmentPOQueryWrapper.eq("ARTICLE_ID", articlePO.getId());
            List<ArticleAttachmentPO> attachmentPOList = attachmentService.list(articleAttachmentPOQueryWrapper);
            ArrayList<Map<String, String>> attachmentMapList = new ArrayList<>();
            if (!ValidateUtil.isNullOrEmpty(attachmentPOList)) {
                for (ArticleAttachmentPO attachmentPO : attachmentPOList) {
                    Map<String, String> attachmentMap = new HashMap<>();
                    attachmentMap.put("name", attachmentPO.getAttachmentName());

                    attachmentMap.put("url", attachmentPO.getAttachmentUrl());

                    attachmentMapList.add(attachmentMap);
                }
            }

            for (Map<String, String> attachmentMap : attachmentMapList) {
                this.binds(attachmentMap);
                this.doBodyRender();
            }
        }

    }


    /**
     * 标签文档
     *
     * @return
     */
    public static TagDocObject doc() {
        TagDocObject tagDocObject = new TagDocObject();
        tagDocObject.setTagName("articleAttachmentList");
        tagDocObject.setDesc("文章附件列表标签");
        tagDocObject.setExample("<#articleAttachmentList serverUrl=\"${serverUrl}\" pageType=\"${pageType}\" articleId=\"${data.articleId!}\" var=\"data\">" +
                "                        <li>\n" +
                "                            <a href=\"${data.url!}\" target=\"_blank\">\n" +
                "                               ${data.name!}\n" +
                "                            </a>\n" +
                "                        </li>\n" +
                "                    </#articleAttachmentList>");

        tagDocObject.setParamDesc("articleId：文章id\n" +
                "serverUrl：资讯发布系统的域名\n" +
                "pageType：用于标识是生成静态页面还是预览（预览：checkPage，生成：null或staticPage）\n" +
                "--------- var: 标签的数据结果集 ---------\n" +
                "id：相关文章id\n" +
                "url：下载地址\n");

        tagDocObject.setOtherDesc("无");

        return tagDocObject;
    }

}
